﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EFB.Lib.Entity;
using EFB.Lib.Repository;

namespace EFB.Lib.Process
{
    public class OrdProdProcess
    {
        private OrdProdRepository Repository = null;

           public OrdProdProcess()
           {

               Repository = new OrdProdRepository();
        }

           public void Incluir(OrdProdEntity OperacaoProd)
           {
            try
            {
                //Repository.Incluir(OperacaoProd);
                Repository.Incluir_Op(OperacaoProd);
                Repository.CommitTransaction();

            }
            catch (Exception Except)
            {               
                Repository.RollbackTransaction();
                throw new Exception(Except.Message);
            
            }
            finally {
                Repository.DatabaseDisconnect();
            }
          

            
         
            

        }

           public void reserva_Op(OrdProdEntity OperacaoProd)
           {
               try
               {
                   int itemidentif_id = 0;

                   itemidentif_id = Repository.selecionarId_ITEMIDENTIF_Rastreabilidade(OperacaoProd);//

                   if (itemidentif_id != 0)
                   {
                       Repository.CriaFormulacao_OP(OperacaoProd);
                       Repository.AtualizarQtdReserva_ITEMIDENTIF_Rastreabilidade(OperacaoProd, itemidentif_id);
                       Repository.Insert_OPINSUM(OperacaoProd);
                       Repository.Insert_ReservaNaOP(OperacaoProd);

                   }
                   else
                   {
                       Repository.RollbackTransaction();
                       throw new Exception("Item não Encontrado");
                   }
                   Repository.CommitTransaction();

               }
               catch (Exception Except)
               {
                   Repository.RollbackTransaction();
                   throw new Exception(Except.Message);

               }
               finally
               {
                   Repository.DatabaseDisconnect();
               }

           }
    
    }
}
